package common

import (
	"fmt"
	"server/app/common/request"
	"server/app/common/response"
	"server/app/services"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 校验入参，调用 MediaService
func ImageUpload(ctx *gin.Context) {
	var form request.ImageUpload

	intId := ctx.Keys["id"].(int)
	form.UserID = intId

	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	outPut, err := services.MediaService.SaveImage(form)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, outPut)
}

// 校验入参，调用 MediaService
func VideoUpload(ctx *gin.Context) {
	var form request.VideoUpload

	id, ok := ctx.Keys["id"].(string)
	if !ok {
		response.BusinessFail(ctx, "userID断言错误")
		return
	}
	intId, err := strconv.Atoi(id)
	if err != nil {
		response.BusinessFail(ctx, "userID格式错误")
		return
	}

	form.UserID = intId

	fmt.Println("form-userid", &form)

	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	outPut, err := services.MediaService.SaveVideo(form)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, outPut)
}
